/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2008
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections;
using System.Data.OleDb;
using System.Web.UI.WebControls;

namespace DNNContentBuilder.UserControls
{
    /// <summary>
	///		Summary description for ConnectionStringControl.
	/// </summary>
	public class ConnectionStringControl : System.Web.UI.UserControl
	{
		// for localization
		public const string resource = "~/DesktopModules/DNNContentBuilder/App_LocalResources/ConnectionStringControl";

		protected LinkButton			btNewConn;
		protected LinkButton			btUpdate;
		protected LinkButton			btDelete;
		protected LinkButton			btCancel;
		protected Panel				ConnectionStringPanel;
		protected Panel				ConnectionsListPanel;
		protected RadioButtonList		RadioButtonList1;
		protected DataList			ConnectionsList;
		protected LinkButton			btSearchConn;
		protected TextBox				txtTitle;
		protected TextBox				txtString;
		protected LinkButton			btTestConn;
		protected System.Web.UI.HtmlControls.HtmlInputHidden	txtConnId;
		protected Label msgBadConnection;
		protected Label msgGoodConnection;
		protected Label msgTestConn;
		protected DropDownList ddDataBase;

		private readonly ConnectionStringController csController = new ConnectionStringController();
		private readonly DatabaseController dbController = new DatabaseController();

		private   ConnectionStringInfo csInfo = new ConnectionStringInfo();
/*
		private   DatabaseInfo dbInfo = new DatabaseInfo();
*/

		private void Page_Load(object sender, EventArgs e)
		{
			if (IsPostBack) 
			{
				this.btDelete.Attributes.Add("onClick", "javascript:return confirm('" + Commons.GetLocalizedString("label_ConfirmDelete", resource) + "')");
			}

			this.btUpdate.Text		= Commons.GetLocalizedString("button_Update", resource);
			this.btDelete.Text		= Commons.GetLocalizedString("button_Delete", resource);
			this.btCancel.Text		= Commons.GetLocalizedString("button_Cancel", resource);
			this.btNewConn.Text		= Commons.GetLocalizedString("button_Create", resource);
			this.btSearchConn.Text	= Commons.GetLocalizedString("button_Search", resource);

			this.msgGoodConnection.Text = Commons.GetLocalizedString("label_Success", resource);

			this.btTestConn.Text	= Commons.GetLocalizedString("button_Test", resource);

			// Put user code to initialize the page here
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);

			fillList();

		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btSearchConn.Click += new System.EventHandler(this.btSearchConn_Click);
			this.btTestConn.Click += new System.EventHandler(this.btTestConn_Click);
			this.btUpdate.Click += new System.EventHandler(this.btUpdate_Click);
			this.btDelete.Click += new System.EventHandler(this.btDelete_Click);
			this.btCancel.Click += new System.EventHandler(this.btSearchConn_Click);
			this.ConnectionsList.ItemCreated += new System.Web.UI.WebControls.DataListItemEventHandler(this.ConnectionsList_ItemCreated);
			this.btNewConn.Click += new System.EventHandler(this.btNewConn_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


		private void btSearchConn_Click(object sender, EventArgs e)
		{
			clearFields();
		}

		private void btNewConn_Click(object sender, EventArgs e)
		{
			this.ConnectionsListPanel.Visible = false;
			this.ConnectionStringPanel.Visible = true;

			fillList();
		}

		private void btUpdate_Click(object sender, EventArgs e)
		{
			csInfo.Title = txtTitle.Text;
			csInfo.ConnectionString = txtString.Text;
			csInfo.DatabaseId = Int32.Parse(ddDataBase.SelectedValue);

			if (txtConnId.Value != "") 
			{
				csInfo.ConnectionStringId = Int32.Parse(txtConnId.Value);
			}

			if (txtConnId.Value == "") 
			{
				csController.Add(csInfo);
			} 
			else 
			{
				csController.Update(csInfo);
			}
			clearFields();
		}

		private void btDelete_Click(object sender, EventArgs e)
		{
			if (txtConnId.Value != "") 
			{
				csController.Delete(Int32.Parse(txtConnId.Value));
			}
			clearFields();
		}

		private void btTestConn_Click(object sender, EventArgs e)
		{
			msgBadConnection.Visible = false;
			msgGoodConnection.Visible = false;

			OleDbConnection testConnection = new OleDbConnection();

			try 
			{
				testConnection.ConnectionString = txtString.Text;
				testConnection.Open();
			}
			catch (Exception localException)
			{
				msgBadConnection.Visible = true;
				msgBadConnection.Text = localException.Message;
			}
			finally 
			{
				testConnection.Close();
			}

			if (!msgBadConnection.Visible) 
			{
				msgGoodConnection.Visible = true;
			}
		}

#pragma warning disable 168
		private void btCancel_Click(object sender, EventArgs e)
#pragma warning restore 168
		{
			clearFields();
		}
		private void clearFields () {
			txtTitle.Text = "";
			txtString.Text= "";
			txtConnId.Value = "";
            
			this.ConnectionsListPanel.Visible = true;
			this.ConnectionStringPanel.Visible = false;

			this.msgBadConnection.Visible = false;
			this.msgGoodConnection.Visible = false;

			fillList();
		}

		private void fillList () 
		{
			ddDataBase.DataSource = (ICollection)dbController.GetAllDatabases();
			ddDataBase.DataTextField = "Title";
			ddDataBase.DataValueField = "DatabaseId";
            ddDataBase.DataBind();

			ConnectionsList.DataSource = (ICollection)csController.GetAllConnections();
			ConnectionsList.DataBind();

			btDelete.Visible = false;
		}

		private void ConnectionsList_ItemCreated( object sender, DataListItemEventArgs e) 
		{ 
			ListItemType elemType = e.Item.ItemType;

			if ((elemType == ListItemType.Item) || (elemType == ListItemType.AlternatingItem))
			{ 
				ImageButton imgButton = (ImageButton) e.Item.FindControl("btConnSelected"); 
				imgButton.Click += btConnSelected_OnClick; 
			}  
		} 
		private void btConnSelected_OnClick(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			ImageButton btConnSelected = (ImageButton)sender;
			csInfo = (ConnectionStringInfo)csController.GetItem(Int32.Parse(btConnSelected.CommandArgument));

			this.txtConnId.Value = csInfo.ConnectionStringId.ToString();
			this.txtTitle.Text = csInfo.Title;
			this.txtString.Text = csInfo.ConnectionString;
			this.ddDataBase.SelectedValue = csInfo.DatabaseId.ToString();

			this.ConnectionsListPanel.Visible = false;
			this.ConnectionStringPanel.Visible = true;

			btDelete.Visible = true;
		}
	}
}
